package com.xydzxt.www.controller.student;
import com.xydzxt.www.model.Major;
import com.xydzxt.www.model.Regist;
import com.xydzxt.www.service.MajorService;
import com.xydzxt.www.service.RegistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class EditController {
    @Autowired
    RegistService registService;
    @Autowired
    MajorService majorService;

    @GetMapping("/edit")
    public String edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        HttpSession session=req.getSession();
        if (session.getAttribute("sfzh")!=null){
            String idNumber=session.getAttribute("sfzh").toString();
            Regist regist=registService.selectByIdNumber(idNumber);
            req.setAttribute("regist",regist);

            List<Integer> majorIdList=new ArrayList<>();
            List<Major> majorList=new ArrayList<>();

            majorIdList.add(regist.getFirstChoice());
            majorIdList.add(regist.getSecondChoice());
            majorIdList.add(regist.getThirdChoice());
            for (int i=0;i<majorIdList.size();i++){
                majorList.add(majorService.selectById(majorIdList.get(i)));
            }
            req.setAttribute("majorList",majorList);
            return "student/applicationDetails";
        }else {
            resp.sendRedirect("index");
        }
        return "";
    }

    @PostMapping("/edit")
    public String edit1(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        HttpSession session=req.getSession();
        String idNumber=session.getAttribute("sfzh").toString();
        Regist regist=registService.selectByIdNumber(idNumber);
        String highSchool=req.getParameter("highSchool");
        String email=req.getParameter("email");
        String address=req.getParameter("address");
        String phone=req.getParameter("phone");
        String parentPhone=req.getParameter("parentPhone");
        String headmasterPhone=req.getParameter("headmasterPhone");
        regist.setHighSchool(highSchool);
        regist.setEmail(email);
        regist.setAddress(address);
        regist.setPhone(phone);
        regist.setParentPhone(parentPhone);
        regist.setHeadmasterPhone(headmasterPhone);
        boolean a=false;
        if (registService.update(regist)){
            req.setAttribute("msg","更新成功！<a href='edit'>点击查看</a>");
        }else {
            req.setAttribute("msg","更新失败!  <a href='edit'>点击返回</a>");
        }
        return "student/msg";
    }
}


